import {
    baseUrl
} from './base.js'
const request = (params, method) => {
	let st = '加载中';
    let baseData = {
        responseType: 'text',
        loading: true,
        showText:st,
        url: '',
        loading: true,
        header: {
            // 'content-type': 'multipart/form-data',
            'content-type': 'application/json',
            // "content-type": "application/x-www-form-urlencoded",
            // "X-Access-Token": uni.getStorageSync('user').token || '',
            "token": uni.getStorageSync('user').token || '',
        },
        // 固定参数
        data: {

        }

    }
    params = {
        ...baseData,
        ...params
    }
    params.data = {
        ...baseData.data,
        ...params?.data
    }
    let {
        url,
        data,
        loading,
        header,
        showText
    } = params
	
    if (params.loading) {
        uni.showLoading({
            title: showText
        })
    }
	// url = url.includes('http') ? url : baseUrl + url
	if(!url.includes('http')){
		if(url.includes('{{url}}')){
			url = baseUrl + url.split('{{url}}')[1]
		}else{
			url = baseUrl + url
		}
	}
    return new Promise((reslove, reject) => {
        uni.request({
            url,
            data,
            method,
            header,
            responseType: params.responseType,
            success: (res) => {
				console.log(res,'kanstatus')
                // uni.hideLoading()
				if (params.loading) {
					uni.hideLoading()
				}
                uni.stopPullDownRefresh()
                console.log('%c请求结果:', 'color:green;', res.data, url, data, params);
				if (url.includes('/api/user/applySeller')) {
					reslove()
					return
				}
				if (url.includes('/api/User/wechatUserInfo')) {
					reslove(res.data)
					return
				}
				if (url.includes('/api/User/miniwechatUserInfo')) {
					reslove(res.data)
					return
				}
				if (url.includes('api.map.baidu.com')) {
					reslove(res.data.result)
					return
				}
                if (res.statusCode == 200) {
                    if (res.data.code == 1) {
                        reslove(res.data)
                    } else if (res.data.code == 401) {
                        uni.clearStorageSync()
                        uni.reLaunch({
                            url: '/pages/Login/Login'
                        })
                        reject(res.data)
                    }else if (res.data.code == 403) {
						uni.clearStorageSync()
						uni.reLaunch({
						    url: '/pages/Login/Login'
						})
						reject(res.data)
                    } else {
						if(!url.includes('/api/Center/sign_integral')){
							uni.showToast({
							    title: res.data.msg,
							    icon: 'none',
							    mask: true
							})
						}
                        reject(res.data)
                    }
                } else if (res.statusCode == 401) {
					uni.clearStorageSync()
					uni.reLaunch({
					    url: '/pages/Login/Login'
					})
					reject(res.data)
                } else if(res.statusCode == 403){
					uni.clearStorageSync()
					uni.reLaunch({
					    url: '/pages/Login/Login'
					})
				} else if (res.statusCode == 500){
					let tit = '';
					if(uni.getLocale()=='en'){
						tit = 'serverexception'
					}else if(uni.getLocale()=='zh-Hans'){
						tit = '服务端异常'
					}else{
						tit = '服務端异常'
					}
                    uni.showToast({
                        title: tit,
                        icon: 'none',
                        mask: true
                    })
                    reject(res.data)
                }else{
					uni.showToast({
						title: res.data.msg,
						icon: 'none',
						mask: true
					})
					reject(res.data)	
				}
            },
            fail: (err) => {
                // uni.hideLoading()
				if (params.loading) {
					uni.hideLoading()
				}
                uni.stopPullDownRefresh()
                console.log('%c请求失败:', 'color:red;', err, url);
                uni.showToast({
                    title: err.errMsg,
                    // title: '请求失败',
                    icon: 'none',
                    mask: true
                })
                reject(err)
            }
        })
    })
}

const http = {
    // http对象中的方法
    get: async params => {
        return await request(params, 'GET')
    },
    post: async params => {
        return await request(params, 'POST')
    },
    put: async params => {
        return await request(params, 'PUT')
    },
    delete: async params => {
        return await request(params, 'DELETE')
    }
}
export default http;
